﻿namespace  PUBCOM
{
    using System;
    using System.Reflection;

    public abstract class DataConverter
    {
        protected DataConverter()
        {


        }

        /// <summary>
        /// 转化成Boolean型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static bool CBoolean(string input)
        {
            bool flag = false;
            if (string.IsNullOrEmpty(input))
            {
                return flag;
            }
            input = input.Trim();
            if (((string.Compare(input, "true", StringComparison.OrdinalIgnoreCase) != 0) && (string.Compare(input, "yes", StringComparison.OrdinalIgnoreCase) != 0)) && (string.Compare(input, "1", StringComparison.OrdinalIgnoreCase) != 0))
            {
                return flag;
            }
            return true;
        }

        /// <summary>
        /// 转化成任意类型
        /// </summary>
        /// <param name="t">要转化的类型</param>
        /// <param name="D0bj">要转化的对象</param>
        /// <returns></returns>
        public static object CAnyDbType(Type t,object D0bj)
        {
            switch (t.Name)
            { 
                case "Boolean":
                    return CBoolean(D0bj.ToString());
                 case "Byte":
                  return Convert.ToByte(D0bj);
                 case "Char":
                  return Convert.ToChar(D0bj);
                 case "SByte":
                  return Convert.ToSByte(D0bj);
                 case "Int16":
                  return Convert.ToInt16(D0bj);
                 case "Int32":
                  return Convert.ToInt32(D0bj);
                 case "UInt32":
                  return Convert.ToUInt32(D0bj);
                 case "Int64":
                  return Convert.ToInt64(D0bj);
            }
            return D0bj;
        }

        /// <summary>
        /// 转化成时间类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static DateTime CDate(object input)
        {
            if (!Convert.IsDBNull(input) && !object.Equals(input, null))
            {
                return CDate(input.ToString());
            }
            return DateTime.Now;
        }

        /// <summary>
        /// 转化成时间类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static DateTime CDate(string input)
        {
            DateTime now=DateTime.Parse("1900-01-01");
            DateTime.TryParse(input, out now);
            //if (!DateTime.TryParse(input, out now))
            //{
            //    now = DateTime.Now;
            //}
            return now;
        }

        /// <summary>
        /// 转化成时间类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="outTime">时间类型</param>
        /// <returns></returns>
        public static DateTime? CDate(string input, DateTime? outTime)
        {
            DateTime time;
            if (!DateTime.TryParse(input, out time))
            {
                return outTime;
            }
            return new DateTime?(time);
        }

        /// <summary>
        /// 将时间对象转换为时间格式字符串
        /// </summary>
        /// <param name="strDateTime">时间对象</param>
        /// <param name="strFormat">字符串格式（""表示显示全部,"d"短日期,"g"年月日时分,"YM"年月,"CH"中文的年月日,"CHg"中文的年月日时分,"allCH"全中文年月日,"NoYear"中文的月日时分,"NYRS"中文年月日时）</param>
        /// <returns></returns>
        public static string CDate(object input, string strFormat)
        {
            return Check.ToDateTime(input, strFormat);
        }

        /// <summary>
        /// 转化成钱类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static decimal CDecimal(object input)
        {
            return CDecimal(input, 0M);
        }

        /// <summary>
        /// 转化成钱类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static decimal CDecimal(string input)
        {
            return CDecimal(input, 0M);
        }

        /// <summary>
        /// 转化成钱类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static decimal CDecimal(object input, decimal defaultValue)
        {
            if (!Convert.IsDBNull(input) && !object.Equals(input, null))
            {
                return CDecimal(input.ToString(), defaultValue);
            }
            return 0M;
        }

        /// <summary>
        /// 转化成钱类型，值为空则返回默认值
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static decimal CDecimal(string input, decimal defaultValue)
        {
            decimal num;
            if (!decimal.TryParse(input, out num))
            {
                num = defaultValue;
            }
            return num;
        }

        /// <summary>
        /// 转化Double类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static double CDouble(object input)
        {
            return CDouble(input, 0.0);
        }

        /// <summary>
        /// 转化Double类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static double CDouble(string input)
        {
            return CDouble(input, 0.0);
        }

        /// <summary>
        /// 转化Double类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static double CDouble(object input, double defaultValue)
        {
            if (!Convert.IsDBNull(input) && !object.Equals(input, null))
            {
                return CDouble(input.ToString(), defaultValue);
            }
            return 0.0;
        }

        /// <summary>
        /// 转化Double类型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static double CDouble(string input, double defaultValue)
        {
            double num;
            if (!double.TryParse(input, out num))
            {
                return defaultValue;
            }
            return num;
        }

        /// <summary>
        /// 转化整型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <returns></returns>
        public static int CLng(object input)
        {
            return CLng(input, 0);
        }

        /// <summary>
        /// 转化整型
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns></returns>
        public static int CLng(string input)
        {
            return CLng(input, 0);
        }

        /// <summary>
        /// 转化整型
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int CLng(object input, int defaultValue)
        {
            if (!Convert.IsDBNull(input) && !object.Equals(input, null))
            {
                return CLng(input.ToString(), defaultValue);
            }
            return defaultValue;
        }

        /// <summary>
        /// 转化整型
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static int CLng(string input, int defaultValue)
        {
            int num;
            if (!int.TryParse(input, out num))
            {
                num = defaultValue;
            }
            else if (input.IndexOf(".") > 0)
            {
                int index = input.IndexOf(".");
                return int.Parse(input.Substring(0, index));
            }
            return num;
        }

        /// <summary>
        /// 转化成单精度
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static float CSingle(object input)
        {
            return CSingle(input, 0f);
        }

        /// <summary>
        /// 转化成单精度
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <returns></returns>
        public static float CSingle(string input)
        {
            return CSingle(input, 0f);
        }

        /// <summary>
        /// 转化成单精度
        /// </summary>
        /// <param name="input">要转化的对象</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static float CSingle(object input, float defaultValue)
        {
            if (!Convert.IsDBNull(input) && !object.Equals(input, null))
            {
                return CSingle(input.ToString(), defaultValue);
            }
            return 0f;
        }

        /// <summary>
        /// 转化成单精度
        /// </summary>
        /// <param name="input">要转化的字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        public static float CSingle(string input, float defaultValue)
        {
            float num;
            if (!float.TryParse(input, out num))
            {
                num = defaultValue;
            }
            return num;
        }
        public static string FormatKXJS(object obj)
        {
          string ret = "";   
            ret = obj == null || obj == System.DBNull.Value ? "" : "'" + obj.ToString();
            
            return ret;
        }
        public static string FormatData(object obj, string format)
        {
            string ret = "";
            //格式化处理
            #region 文本列进行格式化输出
            if (format != "")
            {
                if (obj == null || obj == System.DBNull.Value)
                {
                    ret = "";
                }
                else
                {
                    switch (obj.GetType().Name)
                    {
                        case "DateTime":
                            ret = ((DateTime)obj).ToString(format);
                            break;
                        case "Double":
                            ret = ((Double)obj).ToString(format);
                            break;
                        case "Decimal":
                            ret = ((Decimal)obj).ToString(format);
                            break;
                        case "Int32":
                            ret = ((Int32)obj).ToString(format);
                            break;
                        case "Int64":
                            ret = ((Int64)obj).ToString(format);
                            break;
                        default:
                            ret = obj.ToString();
                            break;
                    }
                }
            }
            else
            {
                //排除字符类型科学计数
                ret = obj == null || obj == System.DBNull.Value ? "" : "'" + obj.ToString();
            }
            #endregion


            return ret;
        }

 

    }
}

